<template>
  <a-modal
    v-model="visible"
    title="编辑"
    @ok="onSubmit"
    @cancel="close"
    width="1000px"
  >
    <a-form-model
      v-if="visible"
      ref="prefixForm"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >


      <a-form-model-item ref="prefixNext" label="前缀标识" prop="prefixNext">
        <a-input  placeholder="输入前缀" v-model="form.prefixNext" >
          <span slot="addonBefore">{{currentNodePrefix  + '.'}}</span>
          </a-input>
      </a-form-model-item>

      <a-form-model-item ref="gnrlid" label="绑定机构" prop="institutionsOrganizationTb.id">
        <a-select v-model="form.institutionsOrganizationTb.id"  placeholder="选择机构">
          <a-select-option v-for="item in gnrlData" :key="item.id" :value="item.id">
            {{ item.orgname }}
          </a-select-option>
        </a-select>
      </a-form-model-item>

    </a-form-model>
  </a-modal>
</template>
<script>
  import {checkHasBindOrg, checkHasPrefix, savePrefixNetxTb} from '@/api/prefix'
  import {getCurrentNode} from '@/api/node'

  import {getAll} from '@/api/organization'


  export default {
  name: 'Edit',
  data: function() {
    return {
      labelCol: {span: 4},
      wrapperCol: {span: 14},
      other: '',
      visible: false,
      form: {
        id: '',
        institutionsOrganizationTb: {
          id: undefined
        }
      },
      gnrlData: [],
      rules: {
        prefixNext: [
          {required: true, message: '请输入前缀标识', trigger: 'blur'}
        ],
        'institutionsOrganizationTb.id': [
          {required: true, message: '请选择机构', trigger: 'blur'}
        ],
      },
      currentNodePrefix: ''
    }
  },
  mounted() {

  },
  methods: {
    getGnrlData() {
      const _this = this
      getAll().then(res => {
        _this.gnrlData = res.data
      })
    },
    async initData(obj) {
      this.getGnrlData()
      await this.getCurrentNodePrefix()

      this.visible = true
      if(obj.id){
        this.$nextTick(() => {
          this.form = obj
          let splitArray = this.form.prefixNext.split('.');
          this.form.prefixNext = splitArray[splitArray.length -1]
        })
      }
    },
    async onSubmit() {
      this.$refs.prefixForm.validate(valid => {
        if (valid) {
          this.save()
        }
      })
    },

    async save(){
      let prefixFull = this.currentNodePrefix + "." + this.form.prefixNext
      let canSubmit = true;
      await checkHasPrefix(prefixFull,this.form.id).then(res => {
        if(res.data){
          this.$message.error('前缀重复')
          canSubmit = false;
        }
      })

      await checkHasBindOrg(this.form.institutionsOrganizationTb.id,this.form.id).then(res => {
        if(res.data){
          this.$message.error('该机构已绑定前缀')
          canSubmit = false;
        }
      })

      if(!canSubmit){
        return
      }

      let assignForm = Object.assign({}, this.form);
      assignForm.prefixNext = prefixFull

      savePrefixNetxTb(assignForm).then(res => {
        if (res.code === '0') {
          this.$message.success('保存成功')
        } else {
          this.$message.error('保存失败')
        }
      }).finally(() => {
        this.close()
      })
    },
    async getCurrentNodePrefix(){
      await getCurrentNode().then(res => {
        if(res.code === '0'){
          this.currentNodePrefix  = res.data.prefix.prefixNext
        }
      })
    },
    close() {
      this.form = {
        id: '',
        institutionsOrganizationTb: {
          id: undefined
        }
      }
      this.visible = false
      this.$emit('ok')
    }
  }
}
</script>
